<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);

class manage extends admin {
	private $db;
	public $siteid;
	
	function __construct() {
	    parent::__construct();
	    pc_base::load_app_class('global');
	    $this->db = pc_base::load_model('order_model');//加载model
	    $this->admin_db = pc_base::load_model('admin_model');
	    $this->siteid = get_siteid();//获取当前站点
	    $this->admin_userid = param::get_cookie('userid'); // 获取当前登录的管理员ID
	}
	
	public function init() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = "";
		$where.= " status != 4 and siteid=".$this->siteid;
		if ($_GET['order_num'] || $_GET['user_name']|| $_GET['status']) {
			if (!empty($_GET['order_num'])) {
				$order_num = $_GET['order_num'];
				$where.= " and order_num = '$order_num'";
			}
			if ($_GET['user_name']) {
				$user_name = $_GET['user_name'];
				$where.= " and user_name like '%".$user_name."%'";
			}
			if ($_GET['status']) {
				$status = $_GET['status'];
				$where.= " and status = '$status'";
			}
			$order_info = $this->db->listinfo($where,'id DESC',$page,10);
		} else {
			$order_info = $this->db->listinfo($where,'id DESC',$page,10);
		}
		$username = array();
		foreach ($order_info as $k=>$v){
			$username = $this->admin_db->get_one(array('userid'=>$v['updated_by']),'realname');
			$order_info[$k]['username'] = $username['realname'];
		}
		$pages = $this->db->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('order_list');
	}
	
	public function edit(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message not exits'),HTTP_REFERER);
		
		$info = $this->db->get_one(array('id'=>$id));
		if (isset($_POST['dosubmit'])) {
			$up_info = isset($_POST['talent']) ? $_POST['talent'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			$data['name'] = $up_info['name'];
			$data['identification'] = $up_info['identification'];
			$data['update_time'] = date('Y-m-d H:i:s',time());
			$data['updated_by'] = $this->admin_userid;
			$data['siteid'] = $this->siteid;
			$result = $this->db->update($data,array('id'=>$id));
			if ($result) {
                //记录行为日志
                $log_content = '<span style="color:blue;">修改证书</span>[证书id：'.$id.'；学员姓名：'.$up_info['name'].']';
                $this->manage_admin_log($log_content);
				showmessage(L('edit_student_success'), '?m=s_certificate&c=manage&a=init');
			} else {
				showmessage(L('edit_student_error'),HTTP_REFERER);
			}
		} else {
			$show_header = false;
			pc_base::load_sys_class('form', '', 0);
			include $this->admin_tpl('certificate_edit');
		}
	}
	
	public function detail() {
	    $id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message not exits'),HTTP_REFERER);
	    $info = $this->db->get_one(array('id'=>$id));
		$username = $this->admin_db->get_one(array('userid'=>$info['updated_by']),'realname');
		$info['username'] = $username['realname'];
	    $show_header = false;
	    pc_base::load_sys_class('form', '', 0);
	    include $this->admin_tpl('order_detail');
	}
	
	public function del(){
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
	            showmessage(L('message not exists'), HTTP_REFERER);
	        }
	        $id = $_REQUEST['id'];
	        $order_num = $_REQUEST['order_num'];
	        $name = $_REQUEST['name'];
	        if (is_array($id)) {
	            foreach ($id as $k => $v) {
	                if (intval($v)) {
	                    $id[$k] = intval($v);
	                } else {
	                    unset($id[$k]);
	                }
	            }
	            $id = implode('\',\'', $id);
	        }
	        $result = $this->db->update(array(
	            'status' => '4'
	        ), "id in ('$id')");
	        if ($result) {
                //记录行为日志
                $log_content = '<span style="color:red;">删除订单</span>[订单id：'.$order_num.'；下单用户：'.$name.']';
                $this->manage_admin_log($log_content);
	            showmessage(L('del_order_success'), HTTP_REFERER);
	        } else {
	            showmessage(L('del_order_error'), HTTP_REFERER);
	        }
	}
	
	/**
	* 添加学员校验身份证号，防止学员重复
	* @date: 2017年3月15日 上午9:57:24
	* @author: LIYANG
	*/
	public function check_student() {
		$student_id = $_GET['identification'];
		if (isset($student_id)){
			$id = $this->db->get_one(array('identification'=>$student_id,'is_student'=>1,'siteid'=>$this->siteid));
			if ($id['identification'] == $student_id) {
				exit('0');
			} else {
				exit('1');
			}
		} else {
			exit('0');
		}
	}
}